package com.thingsgrid.device.service.impl;

import com.mongodb.client.result.UpdateResult;
import com.thingsgrid.device.entity.Shadow;
import com.thingsgrid.device.service.IShadowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ShadowServiceImpl implements IShadowService {

	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 *说明：添加阈值
	 *@author lilongfei
	 *@since 2019/9/18 16:31
	 */
	@Override
	public Integer insertDeviceDesired(String deviceId, String key, String value) {
		Update update = new Update();
		//插入新对象
		update.set("device_shadow.state.desired."+key,value);
		Query query = new Query(Criteria.where("device_id").is(deviceId));
		UpdateResult updateResult = mongoTemplate.updateFirst(query,update,"device");
		System.out.println(updateResult.isModifiedCountAvailable());
		return Integer.parseInt(String.valueOf(updateResult.getModifiedCount()));
	}

	/**
	 * 新增
	 */
	@Override
	public void saveShadow(Shadow shadow) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = df.format(new Date());
		shadow.setCreateTime(format);
		mongoTemplate.save(shadow);
	}
	/**
	 * 删除
	 */
	@Override
	public void removeShadow(String shadowId) {
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(shadowId));
		mongoTemplate.remove(query,Shadow.class);
	}

	/**
	 * 修改
	 * @param shadow
	 */
	@Override
	public void updateShadow(Shadow shadow) {
		Query query = new Query();
		query.addCriteria(Criteria.where("shadowId").is(shadow.getShadowId()));
		Update update = new Update();
		update.set("shadowId", shadow.getShadowId());
		update.set("deviceId", shadow.getDeviceId());
		//update.set("originalData", shadow.getOriginalData());
		update.set("createTime", shadow.getCreateTime());
		mongoTemplate.updateFirst(query,update,Shadow.class);

	}

	/**
	 * 根据影子id查
	 * @param shadowId
	 * @return
	 */
	@Override
	public Shadow findShadowById(String shadowId) {
		Query query = new Query();
		query.addCriteria(Criteria.where("productId").is(shadowId));
		return mongoTemplate.findOne(query,Shadow.class);
	}

	/**
	 * 查询所有
	 * @return
	 */
	@Override
	public List<Shadow> findAll() {
		return mongoTemplate.findAll(Shadow.class);
	}

	/**
	 * 分页查
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<Shadow> findAll(Integer pageIndex, Integer pageSize) {
		Query query = new Query();
		Pageable pageable = new PageRequest(pageIndex-1, pageSize);
		query.with(pageable);
		return  mongoTemplate.find(query,Shadow.class);
	}

	@Override
	public List<Shadow> selectByCondition(String shadowName, String shadowType, Timestamp beginTime, Timestamp endTime, Integer isPublish) {
		return null;
	}
}
